//存放主站域名
// const BASE_URL = 'http://192.168.110.218:8079/app' //李鑫接口
// const BASE_URL = 'http://192.168.110.80:8079/app' //刘璇接口
// const BASE_URL = 'http://192.168.110.135:8079/app' //梁波接口
// const BASE_URL = 'http://192.168.110.186:8079/app' //张猛接口
// const BASE_URL = 'http://192.168.110.192:8079/app' //牛哥接口		
const BASE_URL = 'http://124.70.14.193:8079/app' //测试环境接口
// const BASE_URL = 'https://app.hxzx365.com/gateway/app' //正式环境接口
const BASE_URL_IMG = BASE_URL + '/img/upload'//上传图片
const request = (url, data = {}, method = "GET", ContentType = 'application/json') => {
	let loadingTimeout;
	return new Promise(function(resolve, reject) {
		let header = {}
		if (uni.getStorageSync('token')) {
			header = {
				'Content-Type': ContentType,
				'Authorization': uni.getStorageSync('token')
			}
		} else {
			header = {
				'Content-Type': ContentType
			}
		}
		console.log("请求参数", data, url)
		if(!data.showLoading || data.showLoading === true){
			loadingTimeout=setTimeout(() => {
				uni.showLoading({
					title: '加载中',
					mask: true
				});
			},1000)
		}
		uni.request({
			url: BASE_URL + url ,
			data,
			method,
			header,
			success: function(res) {
				console.log('res', 'res', res)
				if (res.statusCode == 401) {  
					let fromPage=true
					// 需要验证token的
					uni.redirectTo({
						url: `/pages/login/login?fromPage=${fromPage}`
					})	
					reject(res)
					return
				} 
				//202商品库存不足
				if (res.data.code == 200 || res.data.code == 202) {
					resolve(res.data)
				} else {
					uni.showToast({
						title: res.data.message,
						icon: 'none',
						duration: 2000
					})	
					reject(res)
				}
			},
			fail: function(err) {
				uni.getNetworkType({
					success: function (res) {
						console.log('当前网络状态:', res.networkType);
						if (res.networkType === 'none') {
						  console.log('当前无网络');
						  uni.showToast({
							title: '当前网络不可用，请检查网络连接',
							icon: 'none'
						  });
						  return
						}else{
							uni.showToast({
								title: '加载失败，请稍后重试！',
								icon: 'none',
								duration: 2000
							})
						}
					  }
					});
				reject(err)
			},
			complete: function() {
				console.log('结束')
				clearTimeout(loadingTimeout);
				uni.hideLoading();
			}
		})
	})
}

// 图片
const Upload = (url, source, formData) => {
	return new Promise(function(resolve, reject) {
		let header = {}
		if (uni.getStorageSync('token')) {
			header = {
				// 'Content-Type': ContentType,
				'authorization': uni.getStorageSync('token')
			}
		}
		uni.uploadFile({
			url: BASE_URL + url,
			file: source,
			formData,
			// name,
			header,
			success: function(res) {
				let obj1 = JSON.parse(res.data)
				uni.hideLoading()
				if (obj1.code !== 200) {
					uni.showToast({
						title: obj1.message,
						icon: 'none',
						duration: 2000
					})	
				} else {
					// resolve(obj1)
					uni.showToast({
						title: '上传成功',
						icon: 'success',
						duration: 1000
					})
					resolve(res.data)
				}
			},
			fail: function(err) {
				console.log(JSON.stringify(err), '失败999')
				uni.hideLoading()
				uni.showToast({
					title: '加载失败, 请稍后再试!',
					icon: 'none',
					duration: 2000
				})
			},
			complete: function() {}
		})
	})
}
// 文件上传
const UploadFile = (url, data = {}, source) => {
	return new Promise(function(resolve, reject) {
		// 返回选定照片的本地文件路径列表，tempFilePath可以作为img标签的src属性显示图片
		data['name'] = 'upload_resource'
		let time = Math.floor(new Date().getTime() / 1000)
		let sign = ''
		var params = Object.keys(data).sort();
		for (var ki in params) {
			sign += ((sign.indexOf('=') !== -1) ? '&' : '') + params[ki] + '=' + encodeURIComponent(data[params[ki]]);
		}
		data['sign'] = md5(md5(sign) + 'e8aac119d38cee477e49d0155832b7f4' + time)
		data['time'] = time

		uni.uploadFile({
			url: BASE_URL + url, //仅为示例，非真实的接口地址
			filePath: source.tempFiles[0].path,
			name: 'upload_resource',
			formData: data,
			success: function(res) {
				res.data = JSON.parse(res.data)
				uni.hideLoading()
				// uni.showToast({
				//   title: '图片上传成功',
				//   icon: 'success',
				//   duration: 1000
				// })
				if (data.returnAll) { // 是否返回所有信息（成功或者失败都返回）
					return resolve(res.data)
				}
				if (res.data.state) {
					resolve(res.data)
				} else {
					uni.showToast({
						title: res.data.message,
						icon: 'none'
					})
				}
			},
			fail: function(err) {
				uni.hideLoading()
				uni.showToast({
					title: '加载失败，请退出后重试！',
					icon: 'none',
					duration: 2000
				})
			},
			complete: function() {}
		})
	})
}
// form表单
export {
	BASE_URL,
	BASE_URL_IMG,
	request,
	Upload,
	UploadFile,
}
